package com.woniu.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.constant.OrderConstant;
import com.woniu.dto.OrderPayDto;
import com.woniu.pojo.Order;
import com.woniu.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class OrderConfirmConsumer {

    @Autowired
    private IOrderService orderService;

    @RabbitListener(
            queues = "dead_order_queue"
    )
    public void handlerMsg(@Payload OrderPayDto orderPayDto){

        //把 order 修改
        String orderSn = orderPayDto.getOrderSn();

        LambdaQueryWrapper<Order> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Order::getOrderSn, orderSn);
        Order order = orderService.getOne(queryWrapper);

        if (order.getStatus() .equals(OrderConstant.ORDER_STATUS_UNPAY)){ //修改状态

            // 改成无效状态
            order.setStatus(OrderConstant.ORDER_STATUS_INVALID);
            orderService.updateById(order);

        }
        log.info("消息消费成功 ....");
    }
}
